package leetcode.problems;

import org.junit.Test;

import java.util.Arrays;

/**
 * Created by gmwang on 2020/2/2
 * Replace Elements with Greatest Element on Right Side
 * 元素替换为最大元素的右边
 *
 */
public class ReplaceElementswithGreatestElementonRightSide {
    /**
     * Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
     * 给定一个数组arr，将该数组中的每个元素替换为右边元素中最大的元素，并将最后一个元素替换为-1
     * After doing so, return the array.
     *
     * Example 1:
     *
     * Input: arr = [17,18,5,4,6,1]
     * Output: [18,6,6,6,1,-1]
     *
     **/
    public int[] replaceElements(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int max = 0;
            for (int j = i + 1; j < arr.length; j++) {
                max = Math.max(max, arr[j]);
            }
            arr[i] = max;
        }
        arr[arr.length - 1] = -1;
        return arr;
    }

    @Test
    public void test(){
        int [] arr = {17,18,5,4,6,1};
        int[] elements = replaceElements(arr);
        System.out.println(Arrays.toString(elements));
    }
}
